﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Collections.Specialized;

namespace BizTalk.Tools.SSOStorage
{
    public class SSOAppList
    {
        private readonly string _appListGUID = "{9302D64A-F3DB-4efa-A9A2-D6BA8FC1C69C}";
        private HybridDictionary _appList;
        private HybridDictionary _identifier;
        private static SSOAppList _instance;

        public static SSOAppList Instance
        {
            get
            {
                if (null == _instance)
                    _instance = new SSOAppList();
                return _instance;
            }
        }

        private SSOAppList()
        {
            try
            {
                if (false == SSOConfigManager.ApplicationExists(_appListGUID))
                {
                    var propertiesBag = new SSOPropBag();
                    object objPropValue = "<empty>";
                    propertiesBag.Write("<empty>", ref objPropValue);

                    SSOConfigManager.CreateConfigStoreApplication(
                        _appListGUID,
                        "List of registered applications",
                        "Everyone",
                        "Everyone",
                        propertiesBag,
                        String.Empty);

                    SSOConfigManager.SetConfigProperties(_appListGUID, "ConfigProperties",  propertiesBag);
                }

                string appUserAcct, appAdminAcct, description, contactInfo;
                _appList = SSOConfigManager.GetConfigProperties(_appListGUID, "ConfigProperties", out description, out contactInfo, out appUserAcct, out appAdminAcct);
            }
            catch { }
        }

        private void DeleteAndRecreate()
        {
            try
            {
                var propertiesBag = AppListPropertyBag;
                var maskArray = new ArrayList();

                if (true == SSOConfigManager.ApplicationExists(_appListGUID))
                {
                    SSOConfigManager.DeleteApplication(_appListGUID);
                }

                SSOConfigManager.CreateConfigStoreApplication(
                    _appListGUID,
                    "List of registered applications",
                    "Everyone",
                    "Everyone",
                    propertiesBag,
                    String.Empty);

                SSOConfigManager.SetConfigProperties(_appListGUID, "ConfigProperties", propertiesBag);
            }
            catch { }
        }

        private SSOPropBag AppListPropertyBag
        {
            get
            {
                var bag = new SSOPropBag();
                bag.properties = _appList;
                return bag;
            }
        }

        public HybridDictionary GetAppList()
        {
            try
            {
                if (null == _appList)
                {
                    string appUserAcct, appAdminAcct, description, contactInfo;
                    _appList = SSOConfigManager.GetConfigProperties(_appListGUID, "ConfigProperties", out description, out contactInfo, out appUserAcct, out appAdminAcct);

                    if (null == _appList)
                    {
                        return new HybridDictionary();
                    }
                }

                return _appList;
            }
            catch { return new HybridDictionary(); }
        }

        public void AddAppList(string name)
        {
            try
            {
                if (name.Equals(_appListGUID))
                    return;

                _appList[name] = 1;
                DeleteAndRecreate();
            }
            catch { }
        }

        public void DeleteAppList(string name)
        {
            try
            {
                if (name.Equals(_appListGUID))
                    return;

                _appList.Remove(name);
                DeleteAndRecreate();
            }
            catch { }
        }
    }
}
